package com.dnxh.config.exception;

import com.dnxh.constant.CustomResult;
import org.apache.shiro.ShiroException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author 枳湫
 * @Time 2022/4/6 17:03
 * @Describe Basic_jar
 **/
@RestControllerAdvice
public class GlobalException {
	
	@ResponseBody
	@ExceptionHandler (Exception.class)
	public CustomResult handler (Exception e) {
		e.printStackTrace ();
		if (e instanceof ShiroException) {
			return CustomResult.fail ("您暂无操作权限!");
		} else if (e instanceof MethodArgumentNotValidException) {
			MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) e;
			return CustomResult.fail (methodArgumentNotValidException.getBindingResult ().getAllErrors ().get (0).getDefaultMessage ());
		}
		return CustomResult.fail (e.getMessage ());
	}
}
